/**
 * Upload
 * ----------------------------------------
 * 
 * - This class is used only for Struts 2.
 * - This class will upload file XML and validate.
 * 
 * @author Caio Thomas
 * @version 28-10-2013
 */
package control;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import model.constraints.AbstractConstraint;
import model.validator.ValidateSaxParse;

public class Upload  extends ActionSupport  {

	private static final long serialVersionUID = 1L;
	private File file;
    private String contentType;
    private String filename;
    private String option;
    
	private ArrayList<AbstractConstraint> constraints = new ArrayList<AbstractConstraint>();    
	private Map<String, Object> session;
	private HashMap<String, ValidateSaxParse> validate = null;	
	private AbstractConstraint ab;
	
	//aux
	private String error;
	
    @SuppressWarnings("unchecked")
	public String execute() {
        
		session = ActionContext.getContext().getSession();		
        validate = (HashMap<String, ValidateSaxParse>) session.get("validate");        
		if(validate == null)
			return "CONSTRAINT";

		String key = getKey();

		if (validate.get(key).getConstraints().size() == 0)
			return "CONSTRAINT";		
		else if(validate.get(key).getConstraints().size() > 0 && option.equals("upload")){
			
			setError("2");
			if(file == null)
				return "ERROR";
				        
	        ValidateSaxParse aux = validate.get(key);	   
	        validate.put(file.getPath(), aux);
	        validate.remove(key);
	        	        
	        validate.get(getKey()).parse(file.getPath());
	        session.put("validate", validate);	
	        
		} else if (option.equals("last") && ("filename").equals(getKey())) {
			
			error="2";
			return "ERROR";
			
		} else if(validate.get(getKey()).getConstraints().size() > 0 && option.equals("last"))
	        validate.get(getKey()).parse(key);
		else
			return "CONSTRAINT";
 					
        return "SUCCESS";
    }

	public String getKey(){
		String key = new String("filename");
		for (String k : validate.keySet())
			key = k;
		
		return key;
	}    
      
    
	public ArrayList<AbstractConstraint> getConstraints() {
		return constraints;
	}

	public void setConstraints(ArrayList<AbstractConstraint> constraints) {
		this.constraints = constraints;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public HashMap<String, ValidateSaxParse> getValidate() {
		return validate;
	}

	public void setValidate(HashMap<String, ValidateSaxParse> validate) {
		this.validate = validate;
	}

	public AbstractConstraint getAb() {
		return ab;
	}

	public void setAb(AbstractConstraint ab) {
		this.ab = ab;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}


	public void setOption(String option) {
		this.option = option;
	}


	public String getOption() {
		return option;
	}

	public void setError(String error) {
		this.error = error;
	}

	public String getError() {
		return error;
	}
}